/**
* Created by yyr on 2018/3/16
*已投资基金选项卡合伙人认缴
*/
<template>
    <div>
        <div class="ca-m-t-15" v-if="editData.editStatus">
                <div class="ca-title">
                    <h3>合伙人认缴信息</h3>
                    <div class="ca-pull-left ca-m-l-20">
                        <el-date-picker type="date" v-model='editData.fundPartnerEntity.recordingTime' placeholder="选择日期" style="width:150px" class="ca-m-r-15"></el-date-picker>
                    </div>
                    <div class="ca-pull-right">
                        <el-button type="danger" size="medium" @click="deletePartner(editData)">删除</el-button>
                        <el-button type="primary" size="medium" @click="saveEdit(editData)">保存</el-button>
                        <el-button size="medium" @click="cancelEdit">取消</el-button>
                    </div>
                </div>
                <div class="ca-m-t-5">
                    <el-row class="ca-m-t-15">
                        <el-col :span="2">变更原因</el-col>
                        <el-col class="ca-m-l-20" :span="16">
                            <el-input  v-model='editData.fundPartnerEntity.reason' type="textarea" :maxlength="400" placeholder="请输入内容"></el-input>
                        </el-col>
                    </el-row>
                    <table class="ca-table ca-m-t-15">
                        <tr>
                            <th width="10%">合伙人类型</th>
                            <th width="10%">数据来源</th>
                            <th width="10%">合伙人名称</th>
                            <th width="10%">出资分期</th>
                            <th width="10%">缴纳年限</th>
                            <th width="10%">认缴金额(万元)</th>
                            <th width="10%">实缴金额(万元)</th>
                            <th width="10%">持有状态</th>
                            <th width="10%" class="p-tb-0"><button type="button" class="ca-btn ca-btn-round" @click="editPartnerTableData(editData)"><i class="fa fa-plus"></i></button></th>
                        </tr>
                        <tr v-for="(list,index1) in editData.distHistoryList">
                            <td>
                                <el-select v-model="list.type" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.type"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.partnerType" @change="lpSourceChange(index1)" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.lpSource"
                                        :key="item.id"
                                        :label="item.name"
                                        :value="item.id"
                                    ></el-option>
                                </el-select>
                            </td>
                            <td>
                                <el-select v-model="list.customerId"
                                           @change="changePartnerEdit(index1)"
                                           filterable placeholder="请选择" style="width:100%;">
                                    <el-option
                                        v-for="item in list.userSourceArr"
                                        :key="item.id"
                                        :label="item.customerName"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td><el-input placeholder="请输入内容" type="number" v-model="list.stages" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.life" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.subscribed" type="number" placeholder="请输入内容" class="ca-vertical-align-middle"></el-input></td>
                            <td><el-input v-model="list.paid" placeholder="请输入内容" type="number"  class="ca-vertical-align-middle"></el-input></td>
                            <td>
                                <el-select v-model="list.holdingState" placeholder="请选择" style="width:100%">
                                    <el-option
                                        v-for="item in initView.fundHolding"
                                        :label="item.name"
                                        :key="item.id"
                                        :value="item.id">
                                    </el-option>
                                </el-select>
                            </td>
                            <td class="p-tb-0">
                                <button type="button" class="ca-btn ca-btn-round" @click="deletePartnerSingle(list,index1)"><i class="fa fa-remove"></i></button>
                            </td>
                        </tr>
                    </table>
                </div>
                <el-row class="ca-title ca-m-15">
                    <h3>附件</h3>
                    <div class="ca-align-right">
                        <csUpload
                            :fileType="fileType"
                            :businessId="editData.fundPartnerEntity.id"
                            uploadType="all"
                            :getFileUploadedReturnData="getFileUploadedReturnDataFun"
                        ></csUpload>
                    </div>
                </el-row>
                <div class="ca-file-ul">
                    <el-row class="ca-file-row" :gutter="20" v-for="(file, fileIndex) in editData.fileList" :key="fileIndex">
                        <el-col :span="4" class="upload-time">{{file.uploadTime|moment('YYYY-MM-DD')}}</el-col>
                        <el-col :span="1">
                            <tz-icon-file-type class="ca-m-r-5" :fileType="file.fileName"></tz-icon-file-type>
                        </el-col>
                        <el-col :span="8">
                            <div class="ca-text-over-flow-ellipsis"
                                 @click="previewFileCheck(file.id, file.fileName)">
                                <span class="ca-font-size-16 ca-pointer">{{file.fileName}}</span>
                            </div>
                        </el-col>
                        <el-col :span="4">
                            <div class="ca-text-over-flow-ellipsis">{{file.uploadUserName}}</div>
                        </el-col>
                        <el-col :span="3" class="ca-pull-right ca-align-right">
                            <a class="ca-download ca-m-r-10" @click="downloadFile(file.id)"></a>
                            <el-button class="ca-table-btn" @click="deleteFile(file.id,fileIndex)">
                                <i class="fa fa-times"></i>
                            </el-button>
                        </el-col>
                    </el-row>
                </div>
            </div>
    </div>
</template>

<script type="text/ecmascript-6">
    import {api} from '../api';
    import {STATUS_HTTP_SUCCESS, apiHttp} from '__service/csHttp';
    import mined from '__components/file-list/mixin';
    export default {
        mixins: [mined],
        data() {
            return {
                fundPartner: [],
                initView: {
                    type: this.$store.state.csConst.PARTNER_TYPE, //合伙人类型
                    fundHolding: this.$store.state.user.dictionaryMap.fundHolding,//持有状态
                    userOptions: this.$store.state.user.guest,//客户
                    lpSource: this.$store.state.user.dictionaryMap.lpSource,
                },
                partnerList: { //+号点击添加表格一行
                    attribute: '',  //资金属性
                    customerId: '', //合伙人Id
                    customerName: '',//合伙人name
                    holdingState: '',//持有状态 fundHolding
                    id: '',  //
                    life: '',//缴纳年限
                    paid: 0,//实缴金额
                    partnerId: '', //已设立基金合伙人主id
                    partnerType: null, //合伙人类型1 客户，2 母基金
                    stages: '', //出资分期
                    subscribed: '', //认缴金额
                    type: 0, //合伙人类型1 Lp 2 Gp
                },
                customerId: '',
                fileType: '',
                userSource: [],//存放对应客户的数组
                userName: this.$store.state.user.cnName,
            }
        },
        props: {
            editData: {
                type: Object,
                default() {
                    return {}
                }
            },
            editIndex: {
                type: Number,
                default() {
                    return 0
                }
            },
            isMotherFund: {
                type: Boolean,
                default() {
                    return false
                }
            }
        },
        created() {
            if (this.isMotherFund === true || !this.FOF) {
                this.initView.lpSource = [{id: '1', name: '机构客户'}, {id: '0', name: '个人客户'}];
            }
            this.editData.distHistoryList.map( item => {
                apiHttp(api.GET_PARTNER_SOURCE,{fundState: item.partnerType}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        item.userSourceArr = ret.data;
                    }
                })
            })
        },
        mounted() {
            let fileTypeList = this.$store.state.user.fileFloderDicVoList;
            fileTypeList.map(item=>{
                if (item.name === '合伙人认缴') {
                    this.fileType = item.type;
                }
            })
        },
        methods: {
            editPartnerTableData(row) {
                if (!this.isMotherFund) {
                    row.distHistoryList.push(Object.assign({},this.partnerList))
                } else {
                    row.distHistoryList.push(Object.assign({},this.partnerList))
                }
            },
            //删除表格中一行
            deletePartnerSingle(list,index) {
                if (list.id) {
                    this.$confirm('是否删除此条合伙人信息?', '提示', {
                        cancelButtonText: '取消',
                        confirmButtonText: '确定',
                        type: 'warning'
                    }).then(async () => {
                        apiHttp(api.DELETE_SINGLE_PARTNER,{id: list.id}).then( ret=>{
                            if (ret.resCode === STATUS_HTTP_SUCCESS) {
                                this.$message({
                                    type: 'success',
                                    message: '删除成功!',
                                    center: true
                                });
                                this.$emit('refresh')
                                this.cancelEdit();
                            }
                        })
                    });
                } else {
                    this.editData.distHistoryList.splice(index, 1)
                }
            },
//            //编辑改变合伙人
            changePartnerEdit(index) {
                let arrId = this.editData.distHistoryList[index].customerId;
                this.editData.distHistoryList[index].userSourceArr.map( item=>{
                    if (arrId === item.id) {
                        this.editData.distHistoryList[index].customerName = item.customerName;
                    }
                })
            },
            //取消编辑
            cancelEdit() {
                this.$emit('cancelEdit', this.editIndex)
            },
            //删除一个合伙人信息
            deletePartner(row) {
                this.$confirm('是否删除此条合伙人信息?', '提示', {
                    cancelButtonText: '取消',
                    confirmButtonText: '确定',
                    type: 'warning'
                }).then(async () => {
                    apiHttp(api.DELETE_PARTNER,{id: row.fundPartnerEntity.id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            this.cancelEdit();
                            this.$emit('refresh')
                        }
                    })
                });
            },
            //保存编辑
            saveEdit(row) {
                for (let i of row.distHistoryList) {
                    if (i.customerName === '') {
                        this.$message.error('请选择合伙人');
                        return false;
                    }
                }
                let data = Object.assign({},row);
                data.distHistoryList.map(item=>{
                    delete item.existence
                    delete item.proportion
                    delete item.userSourceArr
                })
                let editFileIdData = [];
                data.fileList.map(item=>{
                    editFileIdData.push(item.id)
                })
                let query = {
                    distHistoryList: data.distHistoryList,
                    fundPartnerEntity: data.fundPartnerEntity,
                    fileList: editFileIdData
                }
                apiHttp(api.CHANGE_PARTNER,query).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$message({
                            type: 'success',
                            message: '修改成功!',
                            center: true
                        });
                        this.$emit('refresh')
                        this.$emit('addRedDot')
                        this.cancelEdit();
                    }
                })
            },
            //根据来源选择不同的合伙人
            lpSourceChange(index) {
                this.editData.distHistoryList[index].customerId = '';
                let value = this.editData.distHistoryList[index].partnerType;
                apiHttp(api.GET_PARTNER_SOURCE,{fundState: value}).then( ret=>{
                    if (ret.resCode === STATUS_HTTP_SUCCESS) {
                        this.$set(this.editData.distHistoryList[index], 'userSourceArr', ret.data)
                    }
                })
            },
            // 删除文件
            deleteFile(id, fileIndex) {
                this.$confirm('确认删除此文件?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    showClose: false
                }).then(async () => {
                    apiHttp(api.DELETE_FILE,{id: id}).then( ret=>{
                        if (ret.resCode === STATUS_HTTP_SUCCESS) {
                            this.$message({
                                type: 'success',
                                message: '删除成功!',
                                center: true
                            });
                            this.editData.fileList.splice(fileIndex, 1);
                        }
                    })
                })
            },
            //添加  文件成功返回的数据
            getFileUploadedReturnDataFun(arr) {
                this.editData.fileList = this.editData.fileList.concat(arr)
            },
        },
        components: {
            'csUpload': require('__components/upload-btn/index'),//上传文件组件
        },
    }
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
    .ca-title{
        height:42px;
    }
    table td{
        padding: 5px 5px;
        box-sizing: border-box;
    }
</style>
